package com.beetobe.main.schedule;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import java.util.TimeZone;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import com.beetobe.callib.CalendarLibHelper;
import com.beetobe.callib.CalendarLibHelper.SelectDay;
import com.beetobe.callib.calendar.AsyncInsertCalendar;
import com.beetobe.callib.event.AsyncEditEvent;
import com.beetobe.callib.event.AsyncInsertEvent;
import com.beetobe.callib.event.EventInfo;
import com.beetobe.main.MakeBee;
import com.beetobe.main.R;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

@SuppressLint("ParserError")
public class CalendarActivity extends Activity implements OnTouchListener {

	private final int FROM_INSERT = 1;
	private final int FROM_LOAD = 2;

	private final int FROM_CLIENT = 0;
	private final int FROM_ATTENDANT = 1;
	Calendar Cal, Now;
	GridLayout glCalendar;
	TextView[][] calendarCell2;

	int prevIndCol = -10;
	int prevIndRow = -10;
	int FoM, Year, Month, Max, Today;

	int MaxMonth, MaxYear;
	int MoveMonth = 0;

	int lcdWidth;
	int MODE;

	String calendar_id;

	boolean isOwner = false;
	boolean Edit = false;
	ArrayList<SelectDay> selectDay = new ArrayList<SelectDay>();
	ArrayList<SelectDay> editDay = new ArrayList<SelectDay>();

	// private List<CalendarInfo> calendars = CalendarLibHelper.calednars;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_calendar);
		glCalendar = (GridLayout) findViewById(R.id.Gridlayout_Calendar);
		glCalendar.setOnTouchListener(this);
		CalendarDataSet();
		Intent intent = getIntent();

		MODE = intent.getIntExtra("MODE", 0);
		Log.e("LOG", String.valueOf(MODE));
		if (MODE == 1) {
			MaxMonth = MaxYear = 0;
			TextView txtYearMonth = (TextView) findViewById(R.id.txtmonth);
			txtYearMonth.setText(String.valueOf(Year) + "년 "
					+ String.valueOf(Month + 1) + "월");
			ImageButton transferBtn = (ImageButton) findViewById(R.id.transferButton);

			selectDay.clear();
			editDay.clear();
			calendar_id = intent.getStringExtra("CALENDAR_ID");

			// 이달력의 모든 Event를 가져온다.

			// 모든이벤트를 foreach로 돌면서 event.summary 의 값이 [모임장]어쩌구 이면 해당 날짜를 클릭 할 수
			// 있도록하고 나머지는 �
			for (EventInfo eInfo : CalendarLibHelper.eventList) {
				StringTokenizer tokenizer = new StringTokenizer(
						eInfo.toString(), "ㅫ");
				String date = tokenizer.nextToken();
				String content = tokenizer.nextToken();

				if (content.equals("[OwnerID]" + CalendarLibHelper.accountName)) {
					CalendarLibHelper.attendantList.clear();
					CalendarLibHelper.clientList.clear();
					CalendarLibHelper.isEventChecked = false;
					new AsyncCheckEvent(calendar_id).execute();
					while (!CalendarLibHelper.isEventChecked) {

					}
					
					isOwner = true;
				}
				if (content.equals("[Host]Create")) {
					// 선택 할 수 있도록 만든다.
					StringTokenizer dateToken = new StringTokenizer(date, ".");
					int year = Integer.parseInt(dateToken.nextToken());
					int month = Integer.parseInt(dateToken.nextToken());
					int day = Integer.parseInt(dateToken.nextToken());
					selectDay.add(new SelectDay(year, month, day));

				}
				if (content.equals("[GUEST]" + CalendarLibHelper.accountName)) {
					Edit = true;
					StringTokenizer dateToken = new StringTokenizer(date, ".");
					int year = Integer.parseInt(dateToken.nextToken());
					int month = Integer.parseInt(dateToken.nextToken());
					int day = Integer.parseInt(dateToken.nextToken());
					editDay.add(new SelectDay(year, month, day));

					CalendarLibHelper.selectDay.add(new SelectDay(year, month,
							day));
				}

			}
			// 받아온 SelectDay의 최대 년,달을 찾아와 Next에 제한을 걸기위해 사용
			for (SelectDay sDay : selectDay) {
				if (sDay.getYear() > MaxYear)
					MaxYear = sDay.getYear();
				if (sDay.getMonth() > MaxMonth)
					MaxMonth = sDay.getMonth() - 1;
			}
			ImageButton PrevButton = (ImageButton) findViewById(R.id.Button_PrevM);
			if (MoveMonth <= 0) {

				PrevButton.setVisibility(View.INVISIBLE);
			} else {
				PrevButton.setVisibility(View.VISIBLE);
			}
			PrevButton.setOnClickListener(new ImageButton.OnClickListener() {
				public void onClick(View v) {
					MoveMonth--;
					CalendarDataSet();
					TextView txtYearMonth = (TextView) findViewById(R.id.txtmonth);
					txtYearMonth.setText(String.valueOf(Year) + "년 "
							+ String.valueOf(Month + 1) + "월");
					CalendarRedefine();

					ImageButton PrevButton = (ImageButton) findViewById(R.id.Button_PrevM);
					if (MoveMonth <= 0) {

						PrevButton.setVisibility(View.INVISIBLE);
					} else {
						PrevButton.setVisibility(View.VISIBLE);
					}
					ImageButton NextButton = (ImageButton) findViewById(R.id.Button_NextM);
					if (Month == MaxMonth && Year == MaxYear) {
						NextButton.setVisibility(View.INVISIBLE);
					} else {
						NextButton.setVisibility(View.VISIBLE);
					}

				}
			});

			ImageButton NextButton = (ImageButton) findViewById(R.id.Button_NextM);
			if (Month == MaxMonth && Year == MaxYear) {

				NextButton.setVisibility(View.INVISIBLE);
			} else {
				NextButton.setVisibility(View.VISIBLE);
			}

			NextButton.setOnClickListener(new ImageButton.OnClickListener() {
				public void onClick(View v) {
					MoveMonth++;
					CalendarDataSet();
					TextView txtYearMonth = (TextView) findViewById(R.id.txtmonth);
					txtYearMonth.setText(String.valueOf(Year) + "년 "
							+ String.valueOf(Month + 1) + "월");
					CalendarRedefine();
					ImageButton PrevButton = (ImageButton) findViewById(R.id.Button_PrevM);
					if (MoveMonth <= 0) {
						PrevButton.setVisibility(View.INVISIBLE);
					} else {
						PrevButton.setVisibility(View.VISIBLE);
					}
					ImageButton NextButton = (ImageButton) findViewById(R.id.Button_NextM);
					if (Month == MaxMonth && Year == MaxYear) {
						NextButton.setVisibility(View.INVISIBLE);
					} else {
						NextButton.setVisibility(View.VISIBLE);
					}
				}
			});
			transferBtn.setOnClickListener(new ImageButton.OnClickListener() {
				public void onClick(View v) {
					// ReturnSelectDay();
					// insert해야함
					if (!Edit) {
						new AsyncInsertEvent(CalendarActivity.this,
								CalendarLibHelper.calendarId, FROM_LOAD)
								.execute();

					} else {
						new AsyncEditEvent(CalendarActivity.this,
								CalendarLibHelper.calendarId).execute();
					}
					CalendarLibHelper.isEventInserted = false;
					while (!CalendarLibHelper.isEventInserted) {

					}
					CalendarLibHelper.eventList.clear();
					CalendarLibHelper.selectDay.clear();
					CalendarLibHelper.editDay.clear();
					finish();
				}
			});

		} else if (MODE == 0) {

			TextView txtYearMonth = (TextView) findViewById(R.id.txtmonth);
			txtYearMonth.setText(String.valueOf(Year) + "년 "
					+ String.valueOf(Month + 1) + "월");
			ImageButton transferBtn = (ImageButton) findViewById(R.id.transferButton);

			ImageButton PrevButton = (ImageButton) findViewById(R.id.Button_PrevM);
			if (MoveMonth <= 0) {

				PrevButton.setVisibility(View.INVISIBLE);
			} else {
				PrevButton.setVisibility(View.VISIBLE);
			}
			PrevButton.setOnClickListener(new ImageButton.OnClickListener() {
				public void onClick(View v) {
					MoveMonth--;
					CalendarDataSet();
					TextView txtYearMonth = (TextView) findViewById(R.id.txtmonth);
					txtYearMonth.setText(String.valueOf(Year) + "년 "
							+ String.valueOf(Month + 1) + "월");
					CalendarRedefine();
					ImageButton PrevButton = (ImageButton) findViewById(R.id.Button_PrevM);
					if (MoveMonth <= 0) {
						PrevButton.setVisibility(View.INVISIBLE);
					} else {
						PrevButton.setVisibility(View.VISIBLE);
					}
					ImageButton NextButton = (ImageButton) findViewById(R.id.Button_NextM);
					if (MoveMonth >= 3) {
						NextButton.setVisibility(View.INVISIBLE);
					} else {
						NextButton.setVisibility(View.VISIBLE);
					}
				}
			});

			ImageButton NextButton = (ImageButton) findViewById(R.id.Button_NextM);
			NextButton.setOnClickListener(new ImageButton.OnClickListener() {

				public void onClick(View v) {
					MoveMonth++;
					CalendarDataSet();
					TextView txtYearMonth = (TextView) findViewById(R.id.txtmonth);
					txtYearMonth.setText(String.valueOf(Year) + "년 "
							+ String.valueOf(Month + 1) + "월");
					CalendarRedefine();
					ImageButton PrevButton = (ImageButton) findViewById(R.id.Button_PrevM);
					if (MoveMonth <= 0) {
						PrevButton.setVisibility(View.INVISIBLE);
					} else {
						PrevButton.setVisibility(View.VISIBLE);
					}
					ImageButton NextButton = (ImageButton) findViewById(R.id.Button_NextM);
					if (MoveMonth >= 3) {
						NextButton.setVisibility(View.INVISIBLE);
					} else {
						NextButton.setVisibility(View.VISIBLE);
					}
				}
			});
			transferBtn.setOnClickListener(new ImageButton.OnClickListener() {
				public void onClick(View v) {
					// ReturnSelectDay();
					// insert해야함
					com.google.api.services.calendar.model.Calendar entry = new com.google.api.services.calendar.model.Calendar();
					entry.setSummary(CalendarLibHelper.newCalendarName);
					new AsyncInsertCalendar(CalendarActivity.this, entry)
							.execute();

					CalendarLibHelper.isCalendarInserted = false;

					while (!CalendarLibHelper.isCalendarInserted) {

					}

					// String calendarId = calendars.get(calendars.size() -
					// 1).id;

					new AsyncInsertEvent(CalendarActivity.this,
							CalendarLibHelper.calendarId, FROM_INSERT)
							.execute();
					CalendarLibHelper.isEventInserted = false;
					while (!CalendarLibHelper.isEventInserted) {

					}
					CalendarLibHelper.eventList.clear();
					CalendarLibHelper.selectDay.clear();
					CalendarLibHelper.editDay.clear();
					finish();
				}
			});
		}

		CalendarSet();
	}

	public int getLcdSIzeWidth() {
		return ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
				.getDefaultDisplay().getWidth();
	} // 가로 크기

	public void CalendarDataSet() {
		Cal = Calendar.getInstance();
		Now = new GregorianCalendar(Cal.get(Calendar.YEAR),
				Cal.get(Calendar.MONTH) + MoveMonth, 1);
		Year = Now.get(Calendar.YEAR);
		Month = Now.get(Calendar.MONTH);
		if (Month == 13) {
			Year++;
			Month = 1;
		}
		Max = Now.getActualMaximum(Calendar.DAY_OF_MONTH);
		FoM = Now.get(Calendar.DAY_OF_WEEK);
		Today = Cal.get(Calendar.DAY_OF_MONTH);
		if (MoveMonth != 0)
			Today = 0;
		// Log.w("MAC Today : ", Today + "");
	}

	public void handleGoogleException(final IOException e) {
		// 구현안됨
		Log.w("TAG", e.getMessage());
	}

	public void CalendarRedefine() {
		int i, j, position;
		int MaxMonthDay;

		Cal.set(Year, Month - 1, 1);
		MaxMonthDay = Cal.getActualMaximum(Calendar.DAY_OF_MONTH);

		j = FoM - 2;

		position = 0;
		// 전달의 남는 뒷부분 표시
		for (i = 1; i < FoM; i++, j--, position++) {
			calendarCell2[position / 7][position % 7]
					.setBackgroundResource(R.drawable.disable_selected);
			calendarCell2[position / 7][position % 7].setText(String
					.valueOf(MaxMonthDay - j));
			calendarCell2[position / 7][position % 7]
					.setContentDescription("3");

		}
		j = FoM - 2;

		Log.w("MAC j : ", j + "");
		// 이번 달의 날짜 표시
		for (i = 1; i <= Max; i++, position++, FoM++) {
			calendarCell2[position / 7][position % 7]
					.setText(String.valueOf(i));
			if (position - j < Today) {
				calendarCell2[position / 7][position % 7]
						.setContentDescription("3");
				calendarCell2[position / 7][position % 7]
						.setBackgroundResource(R.drawable.disable_selected);
			} else {

				calendarCell2[position / 7][position % 7]
						.setContentDescription("1");
				calendarCell2[position / 7][position % 7]
						.setBackgroundResource(R.drawable.non_selected);
			}
			if (MODE == 1) {
				MatchDate(Year, Month, position);
			}

		}

		Cal.set(Year, Month + 1, 1);
		// 다음달의 앞에 부분표시
		for (i = FoM + 1; i <= 43; i++, position++) {
			calendarCell2[position / 7][position % 7]
					.setBackgroundResource(R.drawable.disable_selected);
			calendarCell2[position / 7][position % 7].setText(String.valueOf(i
					- FoM));
			calendarCell2[position / 7][position % 7]
					.setContentDescription("3");

		}

		// Next,Prev하기전에 터치한 날짜 불러와 활성화시키기
		for (i = 0; i < CalendarLibHelper.selectDay.size(); i++) {
			if (CalendarLibHelper.selectDay.get(i).getMonth() - 1 == Month
					&& CalendarLibHelper.selectDay.get(i).getYear() == Year) {
				// 이번달에 선택된 날짜가 있다면 탐색후 활성화
				for (position = 0; position <= 41; position++) {
					if (Integer
							.valueOf(calendarCell2[position / 7][position % 7]
									.getText().toString()) == CalendarLibHelper.selectDay
							.get(i).getDay()
							&& Integer
									.valueOf(calendarCell2[position / 7][position % 7]
											.getContentDescription().toString()) == 1) {
						calendarCell2[position / 7][position % 7]
								.setContentDescription("0");
						calendarCell2[position / 7][position % 7]
								.setBackgroundResource(R.drawable.selected);
					}
				}
			}
		}
	}

	public void CalendarSet() {
		int i, j, position;
		int MaxMonthDay;
		int cellSize;
		calendarCell2 = new TextView[6][7];

		Cal.set(Year, Month - 1, 1);
		MaxMonthDay = Cal.getActualMaximum(Calendar.DAY_OF_MONTH);

		j = FoM - 2;

		lcdWidth = getLcdSIzeWidth();
		cellSize = lcdWidth / 7;

		position = 0;
		for (i = 1; i < FoM; i++, j--, position++) {
			calendarCell2[position / 7][position % 7] = new TextView(this);
			calendarCell2[position / 7][position % 7]
					.setBackgroundResource(R.drawable.disable_selected);
			calendarCell2[position / 7][position % 7].setText(String
					.valueOf(MaxMonthDay - j));
			calendarCell2[position / 7][position % 7]
					.setContentDescription("3");
			calendarCell2[position / 7][position % 7].setWidth(cellSize);
			calendarCell2[position / 7][position % 7].setHeight(cellSize);
			calendarCell2[position / 7][position % 7].setGravity(Gravity.TOP);
			calendarCell2[position / 7][position % 7].setGravity(Gravity.LEFT);
			glCalendar.addView(calendarCell2[position / 7][position % 7]);

		}
		j = FoM - 2;

		for (i = 1; i <= Max; i++, position++, FoM++) {
			calendarCell2[position / 7][position % 7] = new TextView(this);
			calendarCell2[position / 7][position % 7]
					.setText(String.valueOf(i));
			calendarCell2[position / 7][position % 7].setGravity(Gravity.TOP);
			calendarCell2[position / 7][position % 7].setGravity(Gravity.LEFT);
			calendarCell2[position / 7][position % 7].setWidth(cellSize);
			calendarCell2[position / 7][position % 7].setHeight(cellSize);
			if (position - j < Today) {
				calendarCell2[position / 7][position % 7]
						.setContentDescription("3");
				calendarCell2[position / 7][position % 7]
						.setBackgroundResource(R.drawable.disable_selected);
			} else {

				calendarCell2[position / 7][position % 7]
						.setContentDescription("1");
				calendarCell2[position / 7][position % 7]
						.setBackgroundResource(R.drawable.non_selected);
			}
			glCalendar.addView(calendarCell2[position / 7][position % 7]);
			if (MODE == 1) {
				MatchDate(Year, Month, position);
			}
		}

		Cal.set(Year, Month + 1, 1);

		for (i = FoM + 1; i <= 43; i++, position++) {
			calendarCell2[position / 7][position % 7] = new TextView(this);
			calendarCell2[position / 7][position % 7]
					.setBackgroundResource(R.drawable.disable_selected);
			calendarCell2[position / 7][position % 7].setText(String.valueOf(i
					- FoM));
			calendarCell2[position / 7][position % 7]
					.setContentDescription("3");
			calendarCell2[position / 7][position % 7].setGravity(Gravity.TOP);
			calendarCell2[position / 7][position % 7].setGravity(Gravity.LEFT);
			calendarCell2[position / 7][position % 7].setWidth(cellSize);
			calendarCell2[position / 7][position % 7].setHeight(cellSize);
			glCalendar.addView(calendarCell2[position / 7][position % 7]);

		}

	}

	private void MatchDate(int Year, int Month, int position) {

		int day = Integer.parseInt(calendarCell2[position / 7][position % 7]
				.getText().toString());

		for (SelectDay sDay : selectDay) {
			if (!(sDay.getYear() == Year && sDay.getMonth() == Month + 1 && sDay
					.getDay() == day)) {
				calendarCell2[position / 7][position % 7]
						.setBackgroundResource(R.drawable.disable_selected);
				calendarCell2[position / 7][position % 7]
						.setContentDescription("3");
			} else {
				if (isOwner) {int attendantCount = 0;
				for (int i = 0; i < CalendarLibHelper.attendantList
						.size(); i++) {
					if (CalendarLibHelper.attendantList.get(i)
							.getYear() == Year
							&& CalendarLibHelper.attendantList
									.get(i).getMonth() == (Month + 1)
							&& CalendarLibHelper.attendantList
									.get(i).getDay() ==
									Integer.valueOf(calendarCell2[position / 7][position % 7].getText().toString())) {
						attendantCount++;
					}
				}
				int attendantRate = (int)((Float.valueOf(Integer.toString(attendantCount))/Float.valueOf(Integer.toString(CalendarLibHelper.clientList.size())))*100);
				
				if(attendantRate > 80) {
					calendarCell2[position / 7][position % 7].setBackgroundResource(R.drawable.selected);
					
				} else if(attendantRate > 60) {
					calendarCell2[position / 7][position % 7].setBackgroundResource(R.drawable.selected_60);
					
				} else if(attendantRate > 40) {
					calendarCell2[position / 7][position % 7].setBackgroundResource(R.drawable.selected_40);
					
				} else if(attendantRate > 20) {
					calendarCell2[position / 7][position % 7].setBackgroundResource(R.drawable.selected_20);
					
				} else if(attendantRate >= 0) {
					calendarCell2[position / 7][position % 7].setBackgroundResource(R.drawable.selected_0);
					
				}
				calendarCell2[position / 7][position % 7]
						.setContentDescription("0");

				} else {
					calendarCell2[position / 7][position % 7]
							.setBackgroundResource(R.drawable.non_selected);
					calendarCell2[position / 7][position % 7]
							.setContentDescription("1");
				}
				break;
			}
		}
		if (Edit) {
			for (SelectDay sDay : editDay) {
				// Log.w("Day : ", String.valueOf(day));
				if (sDay.getYear() == Year && sDay.getMonth() == Month + 1
						&& sDay.getDay() == day) {
					calendarCell2[position / 7][position % 7]
							.setBackgroundResource(R.drawable.selected);
					calendarCell2[position / 7][position % 7]
							.setContentDescription("0");
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_calendar, menu);
		return true;
	}

	public boolean onTouch(View v, MotionEvent event) {

		if (v.getId() == R.id.Gridlayout_Calendar) {
			if (event.getAction() == MotionEvent.ACTION_DOWN
					|| event.getAction() == MotionEvent.ACTION_MOVE) {
				float pntX = event.getX();
				float pntY = event.getY();

				final int indCol = (int) (pntX / calendarCell2[0][0].getWidth());
				final int indRow = (int) (pntY / calendarCell2[0][0]
						.getHeight());
				if (indRow >= 0 && indCol >= 0 && indRow < 6 && indCol < 7) {
					int Day = Integer.valueOf(calendarCell2[indRow][indCol]
							.getText().toString());
					if (prevIndCol != indCol || prevIndRow != indRow) {
						String content = calendarCell2[indRow][indCol]
								.getContentDescription().toString();
						if (content.equals("1")) {
							calendarCell2[indRow][indCol]
									.setBackgroundResource(R.drawable.selected);
							calendarCell2[indRow][indCol]
									.setContentDescription("0");
							// SelectDay에 낳짜 넣기

							CalendarLibHelper.selectDay.add(new SelectDay(Year,
									Month + 1, Day));
							// CalendarLibHelper.editDay
							// .add(new SelectDay(
							// Year,
							// Month + 1,
							// Integer.valueOf(calendarCell2[indRow][indCol]
							// .getText().toString())));
						} else if (content.equals("0")) {
							if (isOwner) {
								ExpandableListAdapter mAdapter;

								ExpandableListView listView = new ExpandableListView(
										this);// (ExpandableListView)
												// CalendarActivity.this.findViewById(R.id.ExpandableListView_progress);
								listView.setOnChildClickListener(new OnChildClickListener() {

									public boolean onChildClick(
											ExpandableListView arg0, View arg1,
											int arg2, int arg3, long arg4) {
										return false;
									}
								});

								listView.setOnGroupClickListener(new OnGroupClickListener() {
									public boolean onGroupClick(
											ExpandableListView parent, View v,
											int groupPosition, long id) {

										return false;
									}
								});
								mAdapter = new ExpandableListAdapter(this,
										new ArrayList<String>(),
										new ArrayList<ArrayList<Group>>());
								Group groupItem = null;

								// 해당날짜의 event를 검색해 캘린더를 등록한 사람들을 '참여함'list에 삽입

								for (int i = 0; i < CalendarLibHelper.attendantList
										.size(); i++) {
									if (CalendarLibHelper.attendantList.get(i)
											.getYear() == Year
											&& CalendarLibHelper.attendantList
													.get(i).getMonth() == (Month + 1)
											&& CalendarLibHelper.attendantList
													.get(i).getDay() == Day) {
										groupItem = new GroupItem_Join(
												CalendarLibHelper.attendantList
														.get(i).getName());
										mAdapter.addItem(groupItem);
									}
								}
								for (int i = 0; i < CalendarLibHelper.clientList.size(); i++) {
									boolean isexist = false;
									for (int j = 0; j < CalendarLibHelper.attendantList
											.size(); j++) {
										if (CalendarLibHelper.attendantList.get(j).getYear() == Year && 
												CalendarLibHelper.attendantList.get(j).getMonth() == (Month + 1)
												&& CalendarLibHelper.attendantList.get(j).getDay() == Day) {
											if (CalendarLibHelper.clientList.get(i)
													.equals(CalendarLibHelper.attendantList.get(j).getName())) {
												isexist = true;
												break;
											}
										}
									}
									if (!isexist) {
										groupItem = new GroupItem_NotJoin(CalendarLibHelper.clientList.get(i).toString());
										mAdapter.addItem(groupItem);
									}

									listView.setAdapter(mAdapter);

									AlertDialog progressDialog = new AlertDialog.Builder(this)
										.setView(listView)
										.setPositiveButton("약속 생성",
											new DialogInterface.OnClickListener() {

												public void onClick(DialogInterface dialog,int which) {
													// TODO
													// Auto-generated
													// method stub
													Intent intent = new Intent(CalendarActivity.this, MakeBee.class);
															intent.putExtra("YEAR", String.valueOf(Year));
															intent.putExtra("MONTH", String.valueOf(Month + 1));
															intent.putExtra("DAY",calendarCell2[indRow][indCol].getText().toString());
															startActivity(intent);
												}
											}).show();
								}
							} else {
								calendarCell2[indRow][indCol]
										.setBackgroundResource(R.drawable.non_selected);
								calendarCell2[indRow][indCol]
										.setContentDescription("1");
								// SelectDay에 낳짜 빼기
								for (int i = 0; i < CalendarLibHelper.selectDay
										.size(); i++) {
									if (CalendarLibHelper.selectDay.get(i)
											.getDay() == Day
											&& CalendarLibHelper.selectDay.get(
													i).getMonth() == Month + 1
											&& CalendarLibHelper.selectDay.get(
													i).getYear() == Year) {
										CalendarLibHelper.selectDay.remove(i);
									}
								}
								// // editDay에 낳짜 빼기
								// for (int i = 0; i < CalendarLibHelper.editDay
								// .size(); i++) {
								// if (CalendarLibHelper.editDay.get(i)
								// .getDay() == Integer
								// .valueOf(calendarCell2[indRow][indCol]
								// .getText().toString())
								// && CalendarLibHelper.editDay.get(
								// i).getMonth() == Month + 1
								// && CalendarLibHelper.editDay.get(
								// i).getYear() == Year) {
								// CalendarLibHelper.editDay.remove(i);
								// }
								// }
							}
							// CalendarLibHelper.selectDay.remove(new
							// SelectDay(Year,
							// Month+1,
							// Integer.valueOf(calendarCell2[indRow][indCol].getText().toString())));
						}
						prevIndCol = indCol;
						prevIndRow = indRow;
						Log.d("Point", "Row : " + indRow + ", Col : " + indCol);
					}
				}

			}
		}
		return true;
	}

	// public void onBackPressed() {
	// finish();
	// }
	@Override
	public void onBackPressed() {
		// CalendarLibHelper.calednars.clear();
		CalendarLibHelper.eventList.clear();
		CalendarLibHelper.selectDay.clear();
		CalendarLibHelper.editDay.clear();

		// Intent intent = new Intent(this, MainActivity.class);
		// startActivity(intent);
		//
		//
		finish();
	}

	// void ReturnSelectDay() {
	// ArrayList<Integer> selectDay = new ArrayList<Integer>();
	// for (int i = 0; i < 6; i++) {
	// for (int j = 0; j < 7; j++) {
	// if (calendarCell2[i][j].getContentDescription() == "0") {
	// selectDay.add(Integer.valueOf(calendarCell2[i][j].getText()
	// .toString()));
	// }
	// }
	// }
	// CalendarLibHelper.selectDay = selectDay;
	// }
}